package com.cn.demo4.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.common.LoadImg;
import com.cn.demo4.common.MyHandlerInterceptor;
import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.NavEntity;
import com.cn.demo4.entity.PageEntity;
import com.cn.demo4.service.INavService;
import com.mysql.cj.xdevapi.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class NavController {
    @Autowired
    private INavService iNavService;
    @RequestMapping("/admin/nav/findAll")
    @ResponseBody
    public void findAll(HttpServletResponse resp, NavEntity navEntity, HttpServletRequest req) throws IOException {
     Tool.setEncoding(req,resp);
        String status = req.getParameter("status");
        status = status == null ? "-1": status;
        navEntity.setStatus(Tool.stringToInt(status));
        PageEntity pageEntity = new PageEntity();
        pageEntity.setCurPage(Tool.toCurPage(req.getParameter("curPage")));
        pageEntity.setPageSize(Tool.toPageSize(req.getParameter("pageSize")));
        navEntity.setPageEntity(pageEntity);
        List<NavEntity> list = iNavService.findAll(navEntity);
        int count = iNavService.getCount(navEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/nav/findById")
    @ResponseBody
    public void findById(HttpServletResponse resp, NavEntity navEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        NavEntity model = iNavService.findById(navEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/nav/add")
    @ResponseBody
    public void add(HttpServletResponse resp, NavEntity navEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        navEntity.setTypeId(Tool.stringToInt((String) req.getAttribute("typeId")));
        navEntity.setHref((String) req.getAttribute("href"));
        navEntity.setName((String) req.getAttribute("name"));
        navEntity.setImg((String) req.getAttribute("img"));
        navEntity.setContent((String) req.getAttribute("content"));
        navEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        String result = iNavService.add(navEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/nav/update")
    @ResponseBody
    public void update(HttpServletResponse resp, NavEntity navEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        navEntity.setId(Tool.stringToInt((String) req.getAttribute("id")));
        navEntity.setName((String) req.getAttribute("name"));
        navEntity.setHref((String) req.getAttribute("href"));
        navEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        navEntity.setTypeId(Tool.stringToInt((String) req.getAttribute("typeId")));
        navEntity.setImg((String) req.getAttribute("img"));
        navEntity.setContent((String) req.getAttribute("content"));
        String result = iNavService.update(navEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/nav/del")
    @ResponseBody
    public void del(HttpServletResponse resp, NavEntity navEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        int result = iNavService.del(navEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
//    @RequestMapping("/getNavModelListForHome")
//    @ResponseBody
//    public void getNavModelListForHome(HttpServletResponse resp, NavEntity navEntity,HttpServletRequest req) throws IOException {
//        Tool.setEncoding(req,resp);
//        List<NavEntity> list = iNavService.getNavModelListForHome(navEntity);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("list",list);
//        resp.getWriter().println(jsonObject);
//    }
}
